package com.binge.test8;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName:IPFinderTest
 * Package:com.binge.test8
 * Description:
 *
 * @Author @Bin
 * @Date 2024/12/27-11:25
 * @Version: v1.0
 */
public class IPFinderTest {

    @Test
    public void testIpFind() {
        Map<String,Object>  map = new HashMap<>();
        map.put("abc",1);
        map.put("def","hello world : [3]");
        map.put("ghi","192.168.110.1");
        map.put("jkl","nettest,[192.168.110.2]");

        map.replaceAll((key, value) -> {
            if (value instanceof String) {
                String stringValue = (String) value;
                // Regular expression to match IP addresses
                String ipRegex = "\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b";
                Pattern pattern = Pattern.compile(ipRegex);
                Matcher matcher = pattern.matcher(stringValue);
                StringBuffer encryptedValue = new StringBuffer();
                while (matcher.find()) {
                    String ipAddress = matcher.group();
                    // Encrypt the IP address (replace this with your actual encryption logic)
                    //String encryptedIp = encryptIpAddress(ipAddress);
                    matcher.appendReplacement(encryptedValue, "encryptedIp");
                }
                matcher.appendTail(encryptedValue);
                return encryptedValue.toString();
            }
            return value;
        });

        System.out.println(map);
    }
}
